package com.xysd.express.activity.login;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xysd.express.R;
import com.xysd.express.activity.main.MainActivity;
import com.xysd.express.base.BaseActivity;
import com.xysd.express.bean.User;
import com.xysd.express.eventbus.RegisterSuccessEvent;
import com.xysd.express.util.ActivityTaskManeger;
import com.xysd.express.util.JSONParser;
import com.xysd.express.util.LoadingDialogUtils;
import com.xysd.express.util.ToastUtil;
import com.xysd.express.util.http.HttpCallBack;
import com.xysd.express.util.http.HttpUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

/**
 * A login screen that offers login via email/password.
 */
@ContentView(R.layout.activity_login)
public class LoginActivity extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.userName_et)
    private EditText userName_et;
    @ViewInject(R.id.pwd_et)
    private EditText pwd_et;
    @ViewInject(R.id.register_tv)
    private TextView register_tv;
    @ViewInject(R.id.forgotPwd_tv)
    private TextView forgotPwd_tv;
    @ViewInject(R.id.agreement_tv)
    private TextView agreement_tv;
    @ViewInject(R.id.login_btn)
    private Button login_btn;

    private String username,pwd;
    @Override
    protected void init() {
        register_tv.setOnClickListener(this);
        forgotPwd_tv.setOnClickListener(this);
        agreement_tv.setOnClickListener(this);
        login_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register_tv:
                startAty(RegisterActivity.class);
                break;
            case R.id.forgotPwd_tv:
                startAty(ForgotPwdActivity.class);
                break;
            case R.id.agreement_tv:

                break;
            case R.id.login_btn:
                getParams();
                login();

                break;
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(RegisterSuccessEvent event){
        username=event.userName;
        pwd=event.password;
        login();
    }

    private void login() {
        if (TextUtils.isEmpty(username)){
            ToastUtil.show("用户名不能为空");
            return;
        }
        if (pwd.length()<6){
            ToastUtil.show("密码不能小于6位");
            return;
        }
        LoadingDialogUtils.createLoadingDialog(mActivity).show();
        HttpUtil.login(username, pwd, new HttpCallBack() {
            @Override
            public void onSuccess(String result) {
                LoadingDialogUtils.closeLoadingDialog();
                Log.i("登录",result);
                String code = JSONParser.getStringFromJsonString("Status",result);
                String msg = JSONParser.getStringFromJsonString("Msg",result);
                if ("0".equals(code)){
                    String data = JSONParser.getStringFromJsonString("Data",result);
                    User user=new Gson().fromJson(data,User.class);
                    User.getInstance().setUser(user);
                    User.getInstance().isLogin=true;
                    startAty(MainActivity.class);
                    ActivityTaskManeger.getInstance().closeActivity(mActivity);
                }else {
                    ToastUtil.show(msg);
                }
            }

            @Override
            public void onFailure(String error) {
                LoadingDialogUtils.closeLoadingDialog();
                Log.e("登录",error);
            }
        });
    }

    private void getParams() {
        username=userName_et.getText().toString();
        pwd=pwd_et.getText().toString();
    }
}

